﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kokomo.Environment;
using System.Collections.ObjectModel;
using System.Windows.Threading;
using System.Windows;
using System.Threading;

namespace Kokomo.Environment.Documents
{
	[ViewType(typeof(TreeDocumentView))]
	public class TreeDocumentViewModel : NavigationDocumentViewModel
	{
		public TreeDocumentViewModel()
		{
			this.HeaderNodes = new ObservableCollection<TreeNodeViewModel>();
		}

		public IList<TreeNodeViewModel> HeaderNodes { get; private set; }

		protected void AddHeaderNode(TreeNodeViewModel node)
		{
			if (node == null) throw new ArgumentNullException("node");

			this.HeaderNodes.DispatcherAdd(node);
		}
		protected void RemoveHeaderNodeFor(object obj)
		{
			TreeNodeViewModel node = this.HeaderNodes.FirstOrDefault(n => n.Target == obj);
			if (node != null)
				this.RemoveHeaderNode(node);
		}
		protected void RemoveHeaderNode(TreeNodeViewModel node)
		{
			if (node == null) throw new ArgumentNullException("node");

			this.HeaderNodes.DispatcherRemove(node);
		}

		private TreeNodeViewModel _selectedNode;
		public TreeNodeViewModel SelectedNode
		{
			get { return this._selectedNode; }
			set
			{
				this._selectedNode = value;

				object target = (value == null) ? null : value.Target;
				this.NavigateTo(target);

				this.OnSelectedNodeChanged();
			}
		}

		public event EventHandler SelectedNodeChanged;
		protected virtual void OnSelectedNodeChanged()
		{
			if (this.SelectedNodeChanged != null) this.SelectedNodeChanged(this, EventArgs.Empty);
			this.OnPropertyChanged("SelectedNode");
		}
	}
}
